/** \file gr_texttools.h */

#pragma once


#include<map>
#include<iterator>
#include<string>
#include<algorithm>

namespace gr {
template<class T>
class Match
{
    /**
        The fastest implementation would probably use a match table like this:
        n = max length (all names)
        k = number of keys
        char [k * ( 4 + m)] table;
        row: id | chacters | fill
          {cccc} {c ... c}   {\0...\0}
        int [k] activeRows ???
    */

public:
    typedef std::map<T, std::string> KeyMap;
    Match(std::map<T, std::string>& map):map_(map)
    {}

    /** Find first match for input string. Return true if match found.
        Return matched type in t.*/
    bool operator()(const std::string& str, T& t)
    {
        bool result = false;
        auto find = [&str](const std::pair<T, std::string>& p)->bool{
            if(str.find(p.second) != std::string::npos) return true; 
            else return false;
        };
        auto end = std::end(map_);
        auto i = std::find_if(std::begin(map_), end, find);
        if(i != end)
        {
            result = true;
            t = i->first;
        }
        return result;
    }
private:
    KeyMap& map_;
};

template<class T>
Match<T> makeMatch(std::map<T, std::string>& map){return Match<T>(map);}

}